<?php
class MessageController extends Zend_Controller_Action {
	
	/**
	 * Send mails from mail_queue periodically.
	 * First, need to have a security check that only allow 
	 * to run the script when ctl and corresponding crc matches.
	 * Second, collect all emails that need to be sent out,
	 * criteria can be send only emails marked as SENT 'false',
	 * can also given timestamp, by default, it only send the mails
	 * created the same day.
	 * 
	 * Example: ctl 1686825444, crc e233847ad0507ae1d0890633ced692b9
	 */
	public function sendemailAction() {
		//$body, $recipients, $from, $subject
		$from = "no-reply@weimingge.com";
		$ctl = $this->_getParam('ctl');
		$crc = $this->_getParam('crc');
		$customerKey = '';
		$startDate = $this->_getParam('start');
		
		$generated_crc = md5($ctl);
		foreach (MailQueue::findUnsentMails() as $mail) {			
			if ($generated_crc == $crc) {
				$db = Zend_Registry::get('db');
				$db->beginTransaction();
				$this->_helper->swiftEmail->sendEmail($mail->message, $mail->recipients, $mail->sender, $mail->subject);
				MailQueue::updateStatus($mail->id);
				$db->commit();
			} else {
				echo 'Not allowed to run the service!';
			}
		}
		exit;
	}
}
?>